package com.example.lib_base.mvvm

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import java.lang.reflect.ParameterizedType

abstract class BaseFragment<VDB : ViewDataBinding, VM : ViewModel> : Fragment() {
    private val type by lazy {
        (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments
    }
     val biding by lazy {
        (type[0] as Class<VDB>).getMethod("inflate", LayoutInflater::class.java)
            .invoke(null, layoutInflater) as VDB
    }
     val vm by lazy {
        val clazz = type[1] as Class<VM>
        ViewModelProvider(this)[clazz]
    }
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        initView()
        initData()

        return biding.root
    }
    abstract fun initView()
    abstract fun initData()


}